//
//  BGViewPager.m
//  TableBarControllerDemo
//
//  Created by 边果互动 on 2021/3/29.
//

#import "BGViewPager.h"
#import "TextUtil.h"

#define barNormalFont [UIFont systemFontOfSize:14]
#define barSelectedFont [UIFont systemFontOfSize:16]

@interface BGViewPager ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *pagerSV;
@property (nonatomic, strong) UIScrollView *barSV;
@property (nonatomic, strong) NSMutableArray *bars;

@end

@implementation BGViewPager

- (BGViewPager *) initWithBarItems: (NSArray *) barItems {
    if (self = [super init]) {
        self.bars = [NSMutableArray array];
        //创建Pager的容器
        CGFloat y = CGRectGetMaxY(self.barSV.frame);
        self.pagerSV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, y, ScreenWidth, ScreenHeight - y)];
        self.pagerSV.backgroundColor = [UIColor greenColor];
        self.pagerSV.showsHorizontalScrollIndicator = NO;
        self.pagerSV.contentSize = CGSizeMake(ScreenWidth * 5, 0);
        self.pagerSV.delegate = self;
        [self.pagerSV setPagingEnabled:YES];
        [self addSubview:self.pagerSV];
        //赋值BarItem内容
        self.barItems = [barItems mutableCopy];
        //创建Bar的滑动布局
        self.barSV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 55)];
        self.barSV.backgroundColor = [UIColor redColor];
        [self addSubview:self.barSV];
        NSInteger count = barItems.count;
        CGFloat marginLeft = 16.0f;
        CGFloat barScrollWidth = 0;
        for (int i = 0; i < count; i++) {
            NSString *title = barItems[i];
            CGSize barSize = [TextUtil text2Size:title andMaxSize:CGSizeMake(MAXFLOAT, 55) andFont:barNormalFont];
            CGFloat barX = marginLeft + i * (barSize.width + marginLeft);
            CGFloat barY = (55 - barSize.height) / 2;
            CGRect barFrame = CGRectMake(barX, barY, barSize.width + 20, barSize.height);
            UIButton *itemBtn = [[UIButton alloc] initWithFrame:barFrame];
            itemBtn.titleLabel.font = barNormalFont;
            [itemBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [itemBtn setTitle:title forState:UIControlStateNormal];
            [self.bars addObject:itemBtn];
            [self.barSV addSubview:itemBtn];
            
            if (i == count - 1) {
                barScrollWidth = CGRectGetMaxX(barFrame) + marginLeft;
            }
            
//            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(150 + ScreenWidth * i, 200, ScreenWidth, ScreenWidth)];
//            label.text = title;
//            label.textColor = [UIColor orangeColor];
////            [self.pagerSV addSubview:label];
//            [self addPager:label];
        }
        self.barSV.contentSize = CGSizeMake(barScrollWidth, 0);
        self.barSV.showsHorizontalScrollIndicator = NO;
        self.barSV.scrollEnabled = NO;
    }
    return self;
}

- (void) addPager: (UIView *) view {
//    [self.pagerViews addObject:view];
    [self.pagerSV addSubview:view];
}

#pragma mark - UIScrollViewDelegate
//UIScrollView手指结束拖拽的时候调用（手指离开的时候）
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    
}

//UIScrollView滑动结束的时候调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat offsetX = scrollView.contentOffset.x;
    int currentPage = offsetX / scrollView.frame.size.width;
    [self changeBars:currentPage];
}

//UIScrollView正在滑动的时候调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
}

- (void) changeBars: (int) position {
    NSInteger count = self.bars.count;
    for (int i = 0; i < count; i++) {
        UIButton *btn = self.bars[i];
        if (i == position) {
            [UIView animateWithDuration:5000 animations:^{
                btn.titleLabel.font = barSelectedFont;
                [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            }];
        } else {
            [UIView animateWithDuration:5000 animations:^{
                btn.titleLabel.font = barNormalFont;
                [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            }];
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
